include('shared.lua')

function GunFactoryMenu(um)
	local factory =  um:ReadShort()
	local self 	= um:ReadEntity()
	if(self.dt.Sparking) then
		local DFrame1 = vgui.Create('DFrame')
		DFrame1:SetSize(185, 91)
		DFrame1:SetPos(181, 119)
		DFrame1:Center()
		DFrame1:SetTitle('Gun Factory')
		DFrame1:MakePopup()
			
		local DLabel1 = vgui.Create('DLabel')
		DLabel1:SetPos(15, 35)
		DLabel1:SetText('Gun Factory is currently in use.')
		DLabel1:SizeToContents()
		DLabel1:SetParent(DFrame1)
		
		local Cancel = vgui.Create('DButton')
		Cancel:SetSize(102, 20)
		Cancel:SetPos(40, 55)
		Cancel:SetParent(DFrame1)
		Cancel:SetText('Cancel Production')
		Cancel.DoClick = function() LocalPlayer():ConCommand("setgunfactoryweapon " .. factory .. " " .. 0) DFrame1:Remove() end
	else
		local FactoryGUI = vgui.Create('DFrame')
		FactoryGUI:SetSize(340, 230)
		FactoryGUI:Center()
		FactoryGUI:SetTitle('Gun Factory')
		FactoryGUI:MakePopup()

		local FactoryList = vgui.Create("DPanelList")
			FactoryList:SetParent(FactoryGUI)
			FactoryList:SetPos(5, 27)
			FactoryList:SetSize(FactoryGUI:GetWide()-10, FactoryGUI:GetTall()-34)
			FactoryList:SetPadding(5)
			FactoryList:SetSpacing(5)
			FactoryList:EnableVerticalScrollbar(true)
				
		for k,v in pairs(gunfactoryweps) do
			if(self.dt.Upgrade < v[1]) then break end
			
			local GunPanel = vgui.Create('DPanel')
				GunPanel:SetParent(FactoryList)
				GunPanel:SetSize(250, 60)

			local GunTitle = vgui.Create('DLabel')
				GunTitle:SetParent(GunPanel)
				GunTitle:SetPos(70, 10)
				GunTitle:SetText(v[2])
				GunTitle:SizeToContents()

			local GunCost = vgui.Create('DLabel')
				GunCost:SetParent(GunPanel)
				GunCost:SetPos(70, 25)
				GunCost:SetText("Production Cost: $" .. v[3])
				GunCost:SizeToContents()

			local GunTime = vgui.Create('DLabel')
				GunTime:SetParent(GunPanel)
				GunTime:SetPos(70, 40)
				GunTime:SetText("Production Time: " .. v[4] / 60 .. " Minutes")
				GunTime:SizeToContents()

			local Produce = vgui.Create('DButton')
				Produce:SetParent(GunPanel)
				Produce:SetSize(55, 20)
				Produce:SetPos(250, 20)
				Produce:SetText('Produce')
				Produce.DoClick = function() LocalPlayer():ConCommand("setgunfactoryweapon " .. factory .. " " .. k) FactoryGUI:Remove() end
				
			local SpawnIcon1 = vgui.Create('SpawnIcon')
				SpawnIcon1:SetParent(GunPanel)
				SpawnIcon1:SetModel(v[5])
				SpawnIcon1:SetToolTip(v[2])				
			FactoryList:AddItem(GunPanel)
		end
	end
end
usermessage.Hook("gunfactorygui", GunFactoryMenu)